//
// Created by Apple on 2021/8/14.
//

#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/async.h"
#include <Logger/Logger.h>

Logger::Logger()
{

}

Logger::~Logger()
{

}

void Logger::init(std::string logFile, std::size_t fileSzie, std::size_t fileCount){
    spdlog::init_thread_pool(8192, 1);
    auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
    auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logFile, fileSzie, fileCount);
    std::vector<spdlog::sink_ptr> sinks {stdout_sink, rotating_sink};
    logger_ = std::make_shared<spdlog::async_logger>("logger", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
    //logger_->set_pattern("[%Y-%m-%d %H:%M:%S.%f][tid %t][%l] [%s:%#] %v");
}

std::shared_ptr<spdlog::logger> Logger::getLogger(){
    return logger_;
}

void Logger::setLevel(LogLevel level){
    logger_->set_level(static_cast<spdlog::level::level_enum>(level));
}

void Logger::setFormat(std::string format){
    logger_->set_pattern(format);
}